﻿using ACC.Business.Stn;
using ACC.Business;
using ACC.Data;
using ACC.Data.Comm;
using ACC.Data.Line;
using ACC.Device;
using System;
using ACC.LineDevice.KukaPack;
using ACC.Data.ACCLog;
using ACC.Data.Entities.Mes;
using System.Collections.Generic;

namespace Script
{
    class L1M02090 : StnProcessPL02_DX
    {
        /// <summary>
        /// 端板涂胶
        /// OP L1M02090 STATION L1M12091, L1M22091
        /// </summary>
        /// <param name="data"></param>
        public L1M02090(StnRunningData data)
            : base(data)
        {
            IsReport = false;
            IsACCReport = false;
            IsPalletRelationSN = false;
        }

        protected override ACCDevice CreateDevice()
        {
            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB190_Offset = 9000;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = 9290;

                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = 9034;
                kukaSiemensDev.DB193_ACC_Offset = 9068;
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = 9034;
                kukaSiemensDev.DB194_PLC_Data_Offset = 9302;

                kukaSiemensDev.BarcodeLength = 30;
                kukaSiemensDev.LoadBarcodeCount = 2;
                kukaSiemensDev.UnloadBarcodeCount = 2;
                kukaSiemensDev.ProcessDataStringDataLength = 8;
                //kukaSiemensDev.ProcessDataStringLength = 2;
                //kukaSiemensDev.ProcessDataRealLength = 20;
                
                kukaSiemensDev.Material_COMM_Offset = 292;       // 上下料信号位偏移量
            }

            return dev;

        }

        protected override void CreateBusinessLogic()
        {
            base.CreateBusinessLogic();

            (_loadProcess as LoadProcess_DX).R = 10;
            (_unloadProcess as UnloadProcess_DX).R = 10;
            (_unloadProcess as UnloadProcess_DX).S = 1;
            (_unloadProcess as UnloadProcess_DX).OneAsNG = true;
        }

        /// <summary>
        /// 从内存中移除（避免后面主线出现问题）
        /// </summary>
        /// <param name="dev"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override ACCCode SendUnloadResult(ACCDevice dev, ACCDevice.UnloadResult result)
        {
            ACCCode rs = base.SendUnloadResult(dev, result);
            if (rs == ACCCode.ACCOK && result.ErrorCode == ACCCode.ACCOK)
            {
                ACC.Business.Process.UnitDataProcess.RemoveWIPUnitFromLineCache(_lineData.RunningData, _data.LastUnloadUnit);
            }

            return rs;
        }
    }
}
